package com.yjhb.android.sbz.dialog;

import android.app.Activity;
import android.text.TextUtils;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

import com.jojo.android.zxlib.base.BaseDialog;
import com.jojo.android.zxlib.tool.ZxDataTool;
import com.jojo.android.zxlib.tool.ZxToast;
import com.yjhb.android.sbz.App;
import com.yjhb.android.sbz.R;
import com.yjhb.android.sbz.entity.UserInfo;
import com.yjhb.android.sbz.imps.BaseTextChangedListener;
import com.yjhb.android.sbz.mvp.settinginfo.SettingInfoPresenter;
import com.yjhb.android.sbz.mvp.settinginfo.SettingInfoView;
import com.yjhb.android.sbz.tool.EventMsg;
import com.yjhb.android.sbz.tool.Tool;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 创建者 zx
 * 创建时间 2021/1/22 9:02
 */
public class BindAliDialog extends BaseDialog implements SettingInfoView {
    private final String TAG = "BindAliDialog";
    @BindView(R.id.et_name)
    EditText etName;
    @BindView(R.id.et_zfb)
    EditText etZfb;
    @BindView(R.id.bind_tv)
    TextView bindTv;
    @BindView(R.id.close_iv)
    ImageView closeIv;
    @BindView(R.id.et_idCard)
    EditText etIdCard;

    SettingInfoPresenter presenter;


    public BindAliDialog(@NonNull Fragment context) {
        super(context);
    }

    public BindAliDialog(Activity context) {
        super(context);
    }

    @Override
    protected void init() {
        presenter = new SettingInfoPresenter(this);
        if (App.userInfo != null) {
            if (!TextUtils.isEmpty(App.userInfo.getZfbName())) {
                etName.setText(App.userInfo.getZfbName());
                etName.setEnabled(false);
            }
            if (!TextUtils.isEmpty(App.userInfo.getIdentityNum())) {
                etIdCard.setText(App.userInfo.getIdentityNum());
                etIdCard.setEnabled(false);
            }
        }
        etName.addTextChangedListener(new BaseTextChangedListener() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                super.onTextChanged(s, start, before, count);
                if (!ZxDataTool.isChineseWord(s.toString())) {
                    //移除监听
                    etName.removeTextChangedListener(this);
                    // 设置处理完的s
                    etName.setText(clearLimitStr(s.toString()));
                    etName.setSelection(etName.getText().length());
                    // 重新添加监听
                    etName.addTextChangedListener(this);
                }
            }

            /**
             * 清除不是中文的内容
             *
             * @return
             */
            private String clearLimitStr(String str) {
                return str.replaceAll("[^[\u4E00-\u9FBF],\u00B7]", "");
            }
        });
        etZfb.addTextChangedListener(new BaseTextChangedListener() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                //移除监听
                etZfb.removeTextChangedListener(this);
                // 设置处理完的s
                etZfb.setText(s.toString().replaceAll("\\s",""));
                etZfb.setSelection(etZfb.getText().length());
                // 重新添加监听
                etZfb.addTextChangedListener(this);
            }
        });
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_bindzfb;
    }

    @OnClick(R.id.close_iv)
    void close() {
        dismiss();
    }

    @OnClick(R.id.bind_tv)
    void bind() {
        if (Tool.isEditEmpty(etName)) {
            ZxToast.showBottomToastLight("请输入姓名！");
            return;
        }
        if (Tool.isEditEmpty(etZfb)) {
            ZxToast.showBottomToastLight("请输入支付宝账号！");
            return;
        }
        if (!verifyZfb(etZfb.getText().toString())) {
            ZxToast.showBottomToastLight("请输入正确的支付宝账号！");
            return;
        }
        if (Tool.isEditEmpty(etIdCard)) {
            ZxToast.showBottomToastLight("请输入身份证号！");
            return;
        }
        if (!ZxDataTool.isIdCard(etIdCard.getText().toString())) {
            ZxToast.showBottomToastLight("身份证号有误，请查正后再输入！");
            return;
        }
        presenter.updateZfbAccount(etName.getText().toString(),
                etZfb.getText().toString(), etIdCard.getText().toString());
        dismiss();
    }

    private boolean verifyZfb(String text) {
        //如果符合手机号正则，则验证通过
        if (ZxDataTool.isMobile(text)) {
            return true;
        }
        //如果有@符号，则验证通过
        if (text.contains("@")) {
            return true;
        }
        return false;
    }


    @Override
    public void onUserInfo(UserInfo userInfo) {

    }

    @Override
    public void onOk() {
        ZxToast.showSuccessBottomToast("绑定成功");
        EventBus.getDefault().post(new EventMsg(EventMsg.REQUEST_USER_UPDATE, null));
    }

    @Override
    public void onWait(String tips) {

    }

    @Override
    public void onZfbChangeSuccess() {

    }

    @Override
    public void onFailure(int code, String msg) {
        ZxToast.showSuccessBottomToast("绑定失败：" + msg);
    }
}
